//
//  HMLikeCollectionCell.m
//  HeartMark
//
//  Created by rimi on 2016/11/14.
//  Copyright © 2016年 黄华强. All rights reserved.
//

#import "HMLikeCollectionCell.h"
#import <AVOSCloud/AVOSCloud.h>

@interface HMLikeCollectionCell ()

@property(nonatomic,strong)UILabel* collectNumber;//收藏的人数
@property(nonatomic,strong)UIImageView* collectImage;//收藏的logo
@property(nonatomic,strong)UILabel* creatTime;//发布时间
@property(nonatomic,strong)UILabel* content;//内容
@property(nonatomic,strong)UILabel* author;//作者

@end


@implementation HMLikeCollectionCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    //发布时间
    _creatTime = [[UILabel alloc] init];
    _creatTime.textColor = [UIColor lightGrayColor];
    _creatTime.font = [UIFont systemFontOfSize:11];
    [self addSubview:_creatTime];
       //详细内容
    _content = [[UILabel alloc] init];
    _content.font = [UIFont systemFontOfSize:13];
    _content.numberOfLines = 10;
    [self addSubview:_content];
    
    //作者名
    _author = [[UILabel alloc] init];
    _author.font = [UIFont systemFontOfSize:11];
    _author.textColor = [UIColor orangeColor];
    [self addSubview:_author];
    
    //设置收藏人数
    _collectNumber = [[UILabel alloc] init];
    _collectNumber.textColor = [UIColor redColor];
    _collectNumber.font = [UIFont systemFontOfSize:13];
    [self addSubview:_collectNumber];
    
    //设置收藏logo
    _collectImage = [[UIImageView alloc] init];
    [self addSubview:_collectImage];
    
}

-(void)setLikeModel:(HMLikeModel *)likeModel{
    _likeModel = likeModel;
    if ([_likeModel.author isEqualToString:@""]) {
        _author.text = @"匿名用户";
    } else {
        _author.text = _likeModel.author;
    }
    
    
    NSString *content = [_likeModel.content stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    _content.text = content;
    _collectNumber.text = [NSString stringWithFormat:@"%@",likeModel.collectNumber];
    //转换日期格式
    NSDateFormatter* df = [[NSDateFormatter alloc]init];//格式化
    [df setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString* s1 = [df stringFromDate:likeModel.updatedAt];
    _creatTime.text = s1;
    
    AVQuery *query = [AVQuery queryWithClassName:@"userIcon"];
    [query whereKey:@"userId" equalTo:_likeModel.authorId];
    [query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        
        if (!error) {
            AVObject *lastObj = objects.lastObject;
            _likeModel.iconImageUrl = lastObj[@"iconUrl"];
            AVFile *file = [AVFile fileWithURL:lastObj[@"iconUrl"]];
            [file getThumbnail:YES width:50 height:50 withBlock:^(UIImage *image, NSError *error) {
                if (error) {
                    _collectImage.image = [UIImage imageNamed:@"默认头像"];
                } else {
                    _collectImage.image = image;
                }
                
            }];

        }
        
        
    }];
    
    
    [self setupFrame];
}

- (void)setupFrame {
    CGFloat contentHeight = _likeModel.cellHeight - 60;
    CGFloat contentWidth = SCREEN_WIDTH/2 - 14;

    _content.frame = CGRectMake(8, 8, contentWidth - 16, contentHeight);
    _collectImage.frame = CGRectMake(8, contentHeight + 20, 15, 15);
    _author.frame = CGRectMake(25, contentHeight + 20, contentWidth - 50, 15);
    _creatTime.frame = CGRectMake(8, contentHeight + 40, contentWidth - 16, 15);
}

@end
